import xml.etree.ElementTree as ET

def main():

    proto = ET.parse('protoy.xml')
    proto_root = proto.getroot()

    ids = []
    conflict_units = []

    for unit in proto_root.findall('Unit'):
        for key in ['id', 'ID', 'Id', 'iD']:
            try:
                uid = unit.attrib[key]
            except KeyError:
                pass

        try:
            uid = int(uid)
        except ValueError:
            conflict_units.append(unit)
        else:
            if uid in ids:
                
                i = 0
                while 1:
                    if i not in ids:
                        break
                    
                    i += 1

                print('Correcting {}, from ID {} to {}.'.format(unit.attrib['name'], uid, i))
                unit.attrib['id'] = str(i)
                ids.append(i)

                conflict_units.append(unit)

            else:
                ids.append(uid)

    for j in filter(lambda i: i not in ids, range(2500)):
        print(j, end=" ")

    print()

    for unit in conflict_units:
        for key in ['name', 'Name']:
            try:
                print(unit.attrib[key], end=" ")
            except KeyError:
                pass

    text = ET.tostring(proto_root)
    text = text.decode().replace('"', "'")
    text = '<?xml version="1.0"?>\n' + text

    with open('protoy2.xml', 'w') as f:
        f.write(text)

    input('')

if __name__ == '__main__':
    try:
        main()
    except:
        import traceback
        traceback.print_exc()
        input('')